﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Dark.Common.Helper;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace DarkCRM.Helpers
{
    public class SqlHelper
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["SqlConString"].ConnectionString;
        public SqlHelper()
        {
            
        }
        private static SqlParameter[] getSqlParameters(IDictionary<string, object> idc)
        {
            SqlParameter[] param = new SqlParameter[idc.Count];
            int i = 0;
            foreach (KeyValuePair<string, object> item in idc)
            {
                param[i] = new SqlParameter("@" + item.Key, item.Value);
                i++;
            }
            return param;
        }
        public static int ExecuteNonQuery(string spName, params SqlParameter[] commandParameters)
        {
            return Dark.Common.Helper.SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
        }

        public static DataSet ExecuteReader(string spName, params SqlParameter[] commandParameters)
        {
            return Dark.Common.Helper.SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
        }
        public static object ExecuteScalar(string spName, params SqlParameter[] commandParameters)
        {
            return Dark.Common.Helper.SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
        }


        public static int ExecuteNonQuery(string spName, IDictionary<string, object> idc)
        {
            return Dark.Common.Helper.SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, getSqlParameters(idc));
        }

        public static DataSet ExecuteReader(string spName, IDictionary<string, object> idc)
        {
            return Dark.Common.Helper.SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, getSqlParameters(idc));
        }
        public static object ExecuteScalar(string spName, IDictionary<string, object> idc)
        {
            return Dark.Common.Helper.SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, getSqlParameters(idc));
        }


        public static int ExecuteNonQuery(string spName)
        {
            return Dark.Common.Helper.SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, null);
        }

        public static DataSet ExecuteReader(string spName)
        {
            return Dark.Common.Helper.SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, null);
        }
        public static object ExecuteScalar(string spName)
        {
            return Dark.Common.Helper.SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, null);
        }
    }
}